package com.store.service;

import com.store.dao.ExpressinfoMapper;
import com.store.model.ExpressinfoModel;
import com.store.model.ResultData;
import com.store.utils.ConstantVariable;
import com.store.utils.DateUtil;
import com.store.utils.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ArrayList;

@Service("expressinfoService")
public class ExpressinfoService {

    @Autowired
    private ExpressinfoMapper expressinfoMapper;

    @Autowired
    private ExcelUtil<ExpressinfoModel> excelUtil;

    public ExpressinfoModel getObjBySfid(String sfOrderId) {
        ExpressinfoModel expressinfoModel = new ExpressinfoModel();
        expressinfoModel.setSforderid(sfOrderId);
        expressinfoModel.setApitype("createorder");
        expressinfoModel.setErrorcode(0);
        return getModelById(expressinfoModel);
    }

    public ExpressinfoModel getObjByOrderNo(String orderNo) {
        ExpressinfoModel expressinfoModel = new ExpressinfoModel();
        expressinfoModel.setOrderno(orderNo);
        expressinfoModel.setApitype("createorder");
        expressinfoModel.setErrorcode(0);
        return getModelById(expressinfoModel);
    }

    public XSSFWorkbook export(ExpressinfoModel model) {
        model.setPageSize(Integer.MAX_VALUE);
        model.setPageNow(0);
        List<ExpressinfoModel> dataList = getList(model);
        return excelUtil.exportExcel(model, dataList);
    }

    public ResultData getData(ExpressinfoModel model) {
        ResultData returnData = new ResultData();
        try {
            returnData.setDataList(this.getList(model));
            int dataCount = this.getCount(model);
            returnData.setPageCount(dataCount, model.getPageSize());
            returnData.setDataCount(dataCount);
            returnData.setState(ConstantVariable.SUCCESS);
            returnData.setPageNumber(model.getPageNumber());
        } catch (Exception e) {
            returnData.setState(ConstantVariable.ERROR);
            returnData.setMsg(e.getMessage());
        }
        return returnData;
    }

    public ExpressinfoModel getModelById(ExpressinfoModel model) {
        List<ExpressinfoModel> dataList = getList(model);
        if (dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }

    public List<ExpressinfoModel> getList(ExpressinfoModel model) {
        return expressinfoMapper.getList(model);
    }

    public int getCount(ExpressinfoModel model) {
        return expressinfoMapper.getCount(model);
    }

    public ExpressinfoModel insert(ExpressinfoModel model) {
        model.setCreatetime(DateUtil.getTime());
        model.setCreateday(DateUtil.getDay());
        model.setShopId(ExpressService.shopId);
        expressinfoMapper.insert(model);
        return model;
    }

    public ExpressinfoModel update(ExpressinfoModel model) {
        expressinfoMapper.update(model);
        return model;
    }

    public List<ExpressinfoModel> batchUpdate(List<ExpressinfoModel> modelList) {
        for (ExpressinfoModel model : modelList) {
            expressinfoMapper.update(model);
        }
        return modelList;
    }

    public void disableOrEnable(ExpressinfoModel model) {
        expressinfoMapper.disableOrEnable(model);
    }

    public void delete(ExpressinfoModel model) {
        List<String> idList = new ArrayList<>();
        idList.add(model.getEsid());
        model.setIdList(idList);
        expressinfoMapper.delete(model);
    }

    public String batchDelete(List<String> idList) {
        ExpressinfoModel model = new ExpressinfoModel();
        model.setIdList(idList);
        expressinfoMapper.delete(model);
        return ResultData.toSuccessString();
    }

}